﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Spider
{
    public class Parse : AttributeList  //继承自AttributeList
    {
        private string sssssss = string.Empty;
        private string m_source;
        private int m_idx;
        private char m_parseDelim;
        private string m_parseName;
        private string m_parseValue;
        public string m_tag;

        public static bool IsWhiteSpace(char ch)
        {
            return ("\t\n\r ".IndexOf(ch) != -1);
        }
        public void EatWhiteSpace()
        {
            while (!Eof())
            {
                if (!IsWhiteSpace(GetCurrentChar()))
                    return;
                m_idx++;
            }
        }
        public bool Eof()
        {
            return (m_idx >= m_source.Length);
        }
        public void ParseAttributeName()
        {
            EatWhiteSpace();
            while (!Eof())
            {
                if (IsWhiteSpace(GetCurrentChar()) || (GetCurrentChar() == '=') || (GetCurrentChar() == '>'))
                    break;
                m_parseName += GetCurrentChar();
                m_idx++;
            }
            EatWhiteSpace();
        }
        public void ParseAttributeValue()
        {
            if (m_parseDelim != 0)
                return;
            if (GetCurrentChar() == '=')
            {
                m_idx++;
                EatWhiteSpace();
                if ((GetCurrentChar() == '\'') || (GetCurrentChar() == '\"'))
                {
                    m_parseDelim = GetCurrentChar();
                    m_idx++;
                    while (GetCurrentChar() != m_parseDelim)
                    {
                        m_parseValue += GetCurrentChar();
                        m_idx++;
                    }
                    m_idx++;
                }
                else
                {
                    while (!Eof() &&
                        !IsWhiteSpace(GetCurrentChar()) && (GetCurrentChar() != '>'))
                    {
                        m_parseValue += GetCurrentChar();
                        m_idx++;
                    }
                }
                EatWhiteSpace();
            }
        }
        public void AddAttribute()
        {
            Attribute a = new Attribute(m_parseName, m_parseValue, m_parseDelim);
            Add(a);
        }
        public char GetCurrentChar()
        {
            char temp = GetCurrentChar(0);
            return temp;
        }
        public char GetCurrentChar(int peek)
        {
            if ((m_idx + peek) < m_source.Length)
            {
                char temp = m_source[m_idx + peek];
                return temp;
            }
            else
                return (char)0;
        }
        /// <summary>
        /// 得到当前字符
        /// </summary>
        /// <returns></returns>
        public char AdvanceCurrentChar()
        {
            char temp = m_source[m_idx++];
            //System.Windows.Forms.MessageBox.Show(temp.ToString());
            //Log.LogSpecial(LogDetail.LogType.Others, temp.ToString());
            sssssss += temp.ToString();
            if (sssssss.Length > 10000)
            {
                //Log.LogSpecial(LogDetail.LogType.Others, sssssss);
            }
            return temp;
        }
        public void Advance()
        {
            m_idx++;
        }
        public string ParseName
        {
            get { return m_parseName; }
            set { m_parseName = value; }
        }
        public string ParseValue
        {
            get { return m_parseValue; }
            set { m_parseValue = value; }
        }
        public char ParseDelim
        {
            get { return m_parseDelim; }
            set { m_parseDelim = value; }
        }

        public string Source
        {
            get { return m_source; }
            set { m_source = value; }
        }
    }
}
